package com.cgw.game.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;


import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.cgw.game.R;

public class SleepStautsView extends View {

    /**
     * 进度画笔
     * */
    private Paint mPaint;
    /**
     * 背景画笔
     * */
    private Paint mBgPaint;
    /**
     * 文字画笔
     * */
    private Paint mTextPaint;

    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;

    /**
     * 背景圆环的宽度
     * */
    private float BgWidth = 30;
    /**
     * 背景圆环的颜色
     * */
    private int outColor;
    /**
     * 背景圆环上圆点的颜色
     * */
    private int outPointColor;

    private Context context;
    /**
     * 内圆环的宽度
     * */
    private float inWidth;

    /**
     * 测试数据
     * */
    private int start =0;
    private int end = 18;
    /**
     * 外圈圆半径
     * */
    private float radius = 0;

    /**
     * 最大刻度
     * */
    private float maxScale = 24;

    /**
     * 每个刻度直接的度数
     * */
    private float angle;

    /**
     * 顶部点击监听
     * */
    private OnClickTopLensenter lensenter;


    /**
     * 头部title点击区域
     * */
    private RectF rectF;

    public SleepStautsView(Context context) {
        this(context,null);
    }

    public SleepStautsView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SleepStautsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        TypedArray ty = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SleepStautsView,0,0);
        try {
            outColor = ty.getColor(R.styleable.SleepStautsView_outColor,getResources().getColor(R.color.sleep_out_bg));
            outPointColor = ty.getColor(R.styleable.SleepStautsView_outPointColor,getResources().getColor(R.color.sleep_out_point_bg));
            BgWidth = ty.getDimension(R.styleable.SleepStautsView_outWidth,DensityUtil.dpToPx(15));
            inWidth = ty.getDimension(R.styleable.SleepStautsView_inWidth,DensityUtil.dpToPx(12));
        }finally {
            ty.recycle();
        }
        initView();
    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(inWidth);
        //
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.WHITE);

       /* bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.home_top_bg);
        bitmapHeight = bitmap.getHeight();
        bitmapWidth = bitmap.getHeight();*/
        angle = 360/maxScale;
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureSize(widthMeasureSpec);
        height = measureSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
        radius = (width>height? height/2:width/2)-DensityUtil.dpToPx(30);
    }
    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = DensityUtil.dip2px(context,350);
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBgCircular(canvas);
        drawBgCircularPoint(canvas);
        drawFrontCircular(canvas);
        drawOutBitmapCircular(canvas);
        drawInTextCircular(canvas);
    }
    /**
     * 背景大圆
     * */
    private void drawBgCircular(Canvas canvas) {
        canvas.translate(width/2,height/2);
        mBgPaint.setColor(outColor);
        mBgPaint.setStrokeWidth(BgWidth);
        mBgPaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(0,0,radius-BgWidth,mBgPaint);
//        canvas.drawArc(-width/2f+BgWidth,-height/2f+BgWidth,width/2f-BgWidth,height/2f-BgWidth,0f,360f,false,mBgPaint);
    }
    /**
     * 外圈图标
     * */
    private void drawOutBitmapCircular(Canvas canvas) {
        mBgPaint.setColor(outPointColor);
        mBgPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle((float)((radius+DensityUtil.dpToPx(10))*Math.cos((start*angle-90f)*Math.PI/180))
                ,(float)((radius+DensityUtil.dpToPx(10))*Math.sin((start*angle-90f)*Math.PI/180)),30,mBgPaint);

        canvas.drawCircle((float)((radius+DensityUtil.dpToPx(10))*Math.cos((range(start,end)*angle-90f)*Math.PI/180))
                ,(float)((radius+DensityUtil.dpToPx(10))*Math.sin((range(start,end)*angle-90f)*Math.PI/180)),30,mBgPaint);
    }

    /**
     * 背景大圆上的圆点
     *  canvas.drawLine((float) (width/2 + width/2 * Math.cos(135f*Math.PI/180)),(float) (height/2 + (width/2) * Math.sin(135f*Math.PI/180)),
     * */
    private void drawBgCircularPoint(Canvas canvas) {
        mBgPaint.setStrokeWidth(10f);
        mBgPaint.setStyle(Paint.Style.FILL);
        canvas.save();
        canvas.rotate(-90f);
        for (int i = 0; i < maxScale; i++) {
            mBgPaint.setColor(outColor);
            canvas.drawLine((float)((radius-BgWidth)*Math.cos((i*angle)*Math.PI/180))
                    ,(float)((radius-BgWidth)*Math.sin((i*angle)*Math.PI/180)),
                    (float)((radius-DensityUtil.dpToPx(30))*Math.cos((i*angle)*Math.PI/180)),
                    (float)((radius-DensityUtil.dpToPx(30))*Math.sin((i*angle)*Math.PI/180)),
                    mBgPaint);
            if(rangeTime(start,end,i)){
                mBgPaint.setColor(outPointColor);
            }else {
                mBgPaint.setColor(outColor);
            }
            canvas.drawCircle((float)((radius-BgWidth)*Math.cos((i*angle)*Math.PI/180))
                    ,(float)((radius-BgWidth)*Math.sin((i*angle)*Math.PI/180)),
                    BgWidth/2-10,mBgPaint);
        }
        canvas.restore();
       /* canvas.save();
        canvas.rotate(-90f);
        for (int i = 0; i < 24; i++) {
            canvas.rotate(i*angle);
            mBgPaint.setColor(outColor);
            canvas.drawLine((float)((radius-BgWidth)*Math.cos(0f*Math.PI/180))
                    ,(float)((radius-BgWidth)*Math.sin(0f*Math.PI/180)),
                    (float)((radius-DensityUtil.dpToPx(30))*Math.cos(0f*Math.PI/180)),
                    (float)((radius-DensityUtil.dpToPx(30))*Math.sin(0f*Math.PI/180)),
                    mBgPaint);
            *//*if(i>=start&&i<=end){

            }else {
                mBgPaint.setColor(outColor);
            }*//*
            mBgPaint.setColor(outPointColor);
            canvas.drawCircle((float)((radius-BgWidth)*Math.cos(0f*Math.PI/180))
                    ,(float)((radius-BgWidth)*Math.sin(0f*Math.PI/180)),
                    BgWidth/2-10,mBgPaint);
        }
        canvas.restore();*/
    }

    /**
     * 画内部弧线
     * */
    private void drawFrontCircular(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(Color.parseColor("#6FA4FF"));
        canvas.save();
        canvas.rotate(-90f);
        canvas.drawArc(-radius+BgWidth+DensityUtil.dpToPx(25),
                -radius+BgWidth+DensityUtil.dpToPx(25),
                radius-BgWidth-DensityUtil.dpToPx(25),
                radius-BgWidth-DensityUtil.dpToPx(25),
                start*angle,range(start,end)*angle,false,mPaint);
        mPaint.setColor(Color.parseColor("#6E76FF"));
        canvas.drawArc(-radius+BgWidth+DensityUtil.dpToPx(25),
                -radius+BgWidth+DensityUtil.dpToPx(25),
                radius-BgWidth-DensityUtil.dpToPx(25),
                radius-BgWidth-DensityUtil.dpToPx(25),
                3*angle,range(3,5)*angle,false,mPaint);
        mPaint.setColor(Color.parseColor("#E3AF62"));
        canvas.drawArc(-radius+BgWidth+DensityUtil.dpToPx(25),
                -radius+BgWidth+DensityUtil.dpToPx(25),
                radius-BgWidth-DensityUtil.dpToPx(25),
                radius-BgWidth-DensityUtil.dpToPx(25),
                6*angle,range(6,10)*angle,false,mPaint);

        mPaint.setColor(Color.parseColor("#6E76FF"));
        canvas.drawArc(-radius+BgWidth+DensityUtil.dpToPx(25),
                -radius+BgWidth+DensityUtil.dpToPx(25),
                radius-BgWidth-DensityUtil.dpToPx(25),
                radius-BgWidth-DensityUtil.dpToPx(25),
                12*angle,range(12,18)*angle,false,mPaint);
        canvas.restore();
    }
    /**
     * 画内部文字
     * */
    private void drawInTextCircular(Canvas canvas) {
        String text = "太棒了";
        mTextPaint.setStrokeWidth(1f);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(outPointColor);
        mTextPaint.setTextSize(DensityUtil.dpToPx(30));
        int textWidth = (int)mTextPaint.measureText(text);
        canvas.drawText(text,-textWidth/2,0,mTextPaint);


        String time = "2018.12.12";
        mTextPaint.setColor(Color.GRAY);
        mTextPaint.setTextSize(DensityUtil.dpToPx(14));
        int textWidthTime = (int)mTextPaint.measureText(time);
        canvas.drawText(time,-textWidthTime/2,radius/2,mTextPaint);


        String title = "睡眠质量";
        mTextPaint.setColor(Color.GRAY);
        mTextPaint.setTextSize(DensityUtil.dpToPx(16));
        Rect rect = new Rect();
        mTextPaint.getTextBounds(title,0,title.length(),rect);
        float y = (-radius+BgWidth+DensityUtil.dpToPx(25))/2;
        canvas.drawText(title,-rect.width()/2,y,mTextPaint);
        mTextPaint.setStyle(Paint.Style.STROKE);
        canvas.drawRoundRect((-rect.width()/2-DensityUtil.dpToPx(10)),
                y-rect.height()-DensityUtil.dpToPx(8),
                (rect.width()/2+DensityUtil.dpToPx(10)),
                y+DensityUtil.dpToPx(10)
                ,50,50
                ,mTextPaint);
        rectF = new RectF((-rect.width()/2-DensityUtil.dpToPx(10)),
                y-rect.height()-DensityUtil.dpToPx(8),
                (rect.width()/2+DensityUtil.dpToPx(10)),
                y+DensityUtil.dpToPx(10));
    }
    private int range(int start,int end){
        if(start>end){
            return (int)(maxScale-start)+end;
        }
        return end-start;
    }
    private boolean rangeTime(int start,int end,int current){
        if(start>end){
            return current>=start||current<=end;
        }
        return current>=start&&current<=end;
    }
    public interface OnClickTopLensenter{
        void onClick();
    }
    public void setLensenter(OnClickTopLensenter lensenter) {
        this.lensenter = lensenter;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                float x = event.getX();
                float y = event.getX();
                if(rectF.contains(x,y)&&lensenter!=null){
                    lensenter.onClick();
                    return true;
                }
                break;
        }
       return super.onTouchEvent(event);
    }
}
